package com.example.work8_1.Controller;


import com.example.work8_1.Exception.ProductException;
import com.example.work8_1.Service.ProductService;
import com.example.work8_1.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("products")
public class ProductController {


    @Autowired
    ProductService ps;


    @PostMapping("")
    public Product addProduct(@RequestBody Product product) throws ProductException {
        return ps.addProduct(product);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('Administration')")
    public void updateProduct(@PathVariable Integer id,@RequestBody Product product)throws ProductException{
        ps.updateProduct(id,product);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('Administration')")
    public void deleteProduct(@PathVariable Integer id) throws ProductException{
        ps.deleteProduct(id);
    }
}
